//
//  XHHTTPRequest.m
//  XHNetwork
//
//  Created by xinph on 2020/3/11.
//  Copyright © 2020 xinph. All rights reserved.
//

#import "XHHTTPRequest.h"
#import "XHHTTPService.h"

@interface XHHTTPRequest ()
/// 请求参数
@property (nonatomic, readwrite, strong) XHURLParameters *urlParameters;
@end

@implementation XHHTTPRequest

+ (instancetype)requestWithParameters:(XHURLParameters *)parameters{
    return [[self alloc] initRequestWithParameters:parameters];
}

- (instancetype)initRequestWithParameters:(XHURLParameters *)parameters{
    
    self = [super init];
    if (self) {
        self.urlParameters = parameters;
    }
    return self;
}

/**
 *
 *  请求的接口
 *
 *  @return 返回需要请求的接口的URI
 */
- (NSString *)requestUrl
{
    return self.urlParameters.path;
}

/**
 请求的数据类型
 
 @return 两种请求数据类型
 */
- (YTKRequestSerializerType)requestSerializerType
{
    return YTKRequestSerializerTypeJSON;
}

/**
 *
 *  返回请求的方式
 *
 *  @return post请求
 */
- (YTKRequestMethod)requestMethod
{
    if ([self.urlParameters.method isEqualToString:XH_HTTTP_METHOD_GET])
    {
        return YTKRequestMethodGET;
    }
    
    if ([self.urlParameters.method isEqualToString:XH_HTTTP_METHOD_HEAD])
    {
        return YTKRequestMethodHEAD;
    }
    
    if ([self.urlParameters.method isEqualToString:XH_HTTTP_METHOD_POST])
    {
        return YTKRequestMethodPOST;
    }
    
    if ([self.urlParameters.method isEqualToString:XH_HTTTP_METHOD_PUT])
    {
        return YTKRequestMethodPUT;
    }
    
    if ([self.urlParameters.method isEqualToString:XH_HTTTP_METHOD_PATCH])
    {
        return YTKRequestMethodPATCH;
    }
    
    if ([self.urlParameters.method isEqualToString:XH_HTTTP_METHOD_DELETE])
    {
        return YTKRequestMethodDELETE;
    }
    
    return YTKRequestMethodPOST;
}

/**
 *
 *  返回请求的参数
 *
 *  @return 封装好的参数
 */
- (NSDictionary *)requestArgument
{
    return self.urlParameters.parameters;
}

/**
 *
 *  设置head信息
 *
 *  @return 需要设置的head的数据
 */
- (NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary
{
    NSMutableDictionary *headerDic = [NSMutableDictionary dictionaryWithDictionary:[super requestHeaderFieldValueDictionary]];
    //做处理
    
    return headerDic;
}

/**
 *
 *  设置请求超时
 *
 *  @return 多少秒以后超时
 */
- (NSTimeInterval)requestTimeoutInterval
{
    return 30;
}

/**
 *
 *  缓存时间
 *
 *  @return 缓存多少秒
 */

- (NSInteger)cacheTimeInSeconds
{
    return 0;
}



@end

/// 网络服务层分类 方便MHHTTPRequest 主动发起请求
@implementation XHHTTPRequest (XHHTTPService)
/// 请求数据
- (RACSignal *) enqueueResultClass:(Class /*subclass of self-definition*/) resultClass {
    return [[XHHTTPService sharedInstance] enqueueRequest:self resultClass:resultClass];
}

@end
